#include "IPCWndServer.h"
#include "WndForIPC.h"
#include <assert.h>

#define IPCHOSTWNDDEFAULTNAME    L"IPCHostWndDefaultServer"

CIPCWndServer::CIPCWndServer(void) 
    : m_pCallBack(NULL)
    , m_pWndIPC(NULL)
    , m_bRecSameSvrNameMsg(true)
{
    m_strServerName = IPCHOSTWNDDEFAULTNAME;
}

CIPCWndServer::~CIPCWndServer(void)
{
    if (m_pWndIPC)
    {
        delete m_pWndIPC;
        m_pWndIPC = NULL;
    }
}

void CIPCWndServer::set_server_name(const std::wstring& _strName)
{
    m_strServerName = _strName;
}

void CIPCWndServer::set_is_rec_same_server_name_msg(bool bRecSameSvrNameMs)
{
    m_bRecSameSvrNameMsg = bRecSameSvrNameMs;
}

int CIPCWndServer::set_handler(IIPCWndCallBack* _p_callback)
{
    m_pCallBack = _p_callback;
    return 0;
}

HWND CIPCWndServer::get_hwnd() const
{
    if (m_pWndIPC)
    {
        return m_pWndIPC->GetHandle();
    }
    return NULL;
}

int CIPCWndServer::start_listen()
{
    if (m_pCallBack == 0)
    {
        assert(false);
        return -1;
    }
    if (m_strServerName.length() == 0)
    {
        assert(false);
        return -2;
    }
    if (m_pWndIPC)
    {
        delete m_pWndIPC;
        m_pWndIPC = 0;
    }
    m_pWndIPC = new CWndForIPC(m_strServerName, m_bRecSameSvrNameMsg);
    m_pWndIPC->set_handler(m_pCallBack);
    return 0;
}

int CIPCWndServer::stop_listen()
{
    if (m_pWndIPC)
    {
        m_pWndIPC->set_handler(NULL);
        delete m_pWndIPC;
        m_pWndIPC = NULL;
    }
    return 0;
}
